package com.qzimp.fujiancuo.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.qzimp.fujiancuo.entity.*;
import com.qzimp.fujiancuo.entity.vo.ResultCode;
import com.qzimp.fujiancuo.exceptions.ApiException;
import com.qzimp.fujiancuo.mapper.LegendMapper;
import com.qzimp.fujiancuo.mapper.PersonageLegendMapper;
import com.qzimp.fujiancuo.mapper.PersonageMapper;
import com.qzimp.fujiancuo.service.PersonageLegendService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Date;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  服务层实现。
 *
 * @author 梁少君
 * @since 2024-12-21
 */
@Service
public class PersonageLegendServiceImpl extends ServiceImpl<PersonageLegendMapper, PersonageLegend> implements PersonageLegendService {

    @Resource
    private PersonageMapper personageMapper;

    @Resource
    private LegendMapper legendMapper;

    @Resource
    private PersonageLegendMapper personageLegendMapper;

    @Override
    public int add(Integer personageId, Integer legendId) {
        //查询人物和传说
        Personage personage = personageMapper.selectOneById(personageId);
        //如果人物为空，则报错人物不存在
        if (personage == null) {
            throw new ApiException(ResultCode.EXIST_NOT_PERSONAGE);
        }
        Legend legend = legendMapper.selectOneById(legendId);
        //如果文化为空，则报错文化不存在
        if (legend == null) {
            throw new ApiException(ResultCode.EXIST_NOT_LEGEND);
        }
        // 创建并保存关联记录
        /*构建对象*/
        PersonageLegend personageLegend = new PersonageLegend();
        personageLegend.setPersonageId(personageId);
        personageLegend.setLegendId(legendId);
        personageLegend.setCreatedAt(LocalDateTime.now());
        personageLegend.setUpdatedAt(LocalDateTime.now());
        return personageLegendMapper.insert(personageLegend);
    }

    @Override
    public List<PersonageAndLegendVO> getLegendsByKeyWord(String keyword) {
        //通过关键字找到相关人物
        List<Personage> personages = personageMapper.searchPersonages(keyword);
        if (personages.isEmpty()){
            throw new ApiException(ResultCode.EXIST_NOT_PERSONAGE);
        }
        // 使用 stream() 将每个 Cuisine 对象映射到 CuisineCategoryVO
        List<PersonageAndLegendVO> collect = personages.stream()
                .map(item -> {
                    PersonageAndLegendVO personageAndLegendVO = new PersonageAndLegendVO();
                    personageAndLegendVO.setName(item.getName());
                    personageAndLegendVO.setAge(item.getAge());
                    personageAndLegendVO.setBirthday(item.getBirthday());
                    personageAndLegendVO.setState(item.getState());
                    personageAndLegendVO.setSex(item.getSex());
                    // 假设 `Cuisine` 类有 `cuisineCateId`，你可以通过该 ID 查询类别名称
                    Legend legend = legendMapper.selectOneByPersonages(item.getName());
                    if (legend != null) {
                        personageAndLegendVO.setLegend(legend);
                    }
                    return personageAndLegendVO;
                })
                .collect(Collectors.toList());
        return collect;
    }

//    @Override
//    public List<Legend> getLegendsByPersonageId(Integer personageId) {
//        //查询人物
//        Personage personage = personageMapper.selectOneById(personageId);
//        if (personage == null) {
//            throw new ApiException(ResultCode.EXIST_NOT_PERSONAGE);
//        }
//        // 根据人物id查询传奇故事的关联
//        QueryWrapper wrapper = new QueryWrapper();
//        wrapper.eq("personage_id", personageId);
//        List<PersonageLegend> personageLegends = personageLegendMapper.selectListByQuery(wrapper);
//        if (personageLegends == null || personageLegends.isEmpty()) {
//            throw new ApiException(ResultCode.EXIST_NOT_LEGEND);
//        }
//        // 获取人物关联的传奇故事的 ID
//        List<Integer> legendIds = personageLegends.stream().
//                map(PersonageLegend::getLegendId).collect(Collectors.toList());
//        /*通过ids查询关联的传奇故事*/
//        return legendMapper.selectLegendsByLegendIds(legendIds);
//    }
}
